var UserLastMsgIdCookieName = 'SidebarUserMsgLastIdCookie'
var yxgjl_msg_url = 'http://yxgjl.youzu.com/msg'
var home_msg_jsonp_url = 'https://home.youzu.com/jsonp/msg/new'
$(function () {
    setSidebarUserMsgCookie(UserLastMsgIdCookieName, 0, 1);
    loadSidebarUserMsgCount();
    setInterval(loadSidebarUserMsgCount,30000);//每隔30秒刷新
});

function rmPromptBoxPrompt() {
    $(".sideBar-item li:nth-child(1) .promptBox").remove()
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//加载新消息iframe
function loadSidebarNewMsgList() {
    rmPromptBoxPrompt()
    var gameId = getQueryString('gameId');
    var need_msg_url = yxgjl_msg_url + '?gameId='+gameId
    $(".sideBar-item li:nth-child(1) iframe").attr("src", need_msg_url);
    $(".sideBar-item li:nth-child(1) iframe").attr("class", "sideBar-pop");
    $(".sideBar-item li:nth-child(1) .arrow").attr("class", "arrow");
    $(".sideBar-item li:nth-child(1) .arrowBack").attr("class", "arrowBack");
}

//清除cookie
function clearSidebarUserMsgCookie(name) {
    setSidebarUserMsgCookie(name, "", -1);
}

//写cookies
function setSidebarUserMsgCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

//获取cookie
function getSidebarUserMsgCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}

//jsonp获得用户当前未读消息数量
function loadSidebarUserMsgCount() {
    var url = home_msg_jsonp_url;
    $.ajax({
        url: url,
        type: 'POST',
        dataType: "jsonp",
        data: {},
        success: function (json) {
            if (json.status == '200') {
                var new_count = parseInt(json.data['count']);
                if (new_count > 0) {
                    //显示红点
                    $(".sideBar-item li:nth-child(1) .popIco:nth-child(1)").html('<i class="reddot"></i>')
                    //检查是否有更新的消息
                    var last_msg_id_cookie = getSidebarUserMsgCookie(UserLastMsgIdCookieName)
                    if (json.data['last_id'] > last_msg_id_cookie) {
                        //显示条形框 并传入 new_count
                        var html = '<div class="promptBox"><div class="prompt" onclick="rmPromptBoxPrompt()"><a href="javascript:;" style="text-decoration:none;" onclick="loadSidebarNewMsgList()">您有 <b>' + new_count + '</b> 条新消息 ! </a><div class="promptArrow"></div></div></div>';
                        $(".sideBar-item li:nth-child(1) iframe").before(html)
                        //10秒后自动关闭
                        setTimeout(rmPromptBoxPrompt,10000)
                        //清空last_msg_time_cookie
                        clearSidebarUserMsgCookie(UserLastMsgIdCookieName)
                        //写入新cookie
                        setSidebarUserMsgCookie(UserLastMsgIdCookieName, json.data['last_id'], 1)
                    }
                } else {
                    //隐藏红点 隐藏条形框
                    $(".sideBar-item li:nth-child(1) div:nth-child(1)").html('')
                    $(".sideBar-item li:nth-child(1) .promptBox").remove()
                }
            }
        }
    });
}
